package com.ruyuan.eshop.order.api.impl;

import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.utils.ParamCheckUtil;
import com.ruyuan.eshop.order.api.OrderApi;
import com.ruyuan.eshop.order.constants.OrderConstants;
import com.ruyuan.eshop.order.domain.dto.*;
import com.ruyuan.eshop.order.domain.request.*;
import com.ruyuan.eshop.order.exception.OrderBizException;
import com.ruyuan.eshop.order.exception.OrderErrorCodeEnum;
import com.ruyuan.eshop.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author lxg
 * @Description:订单中心接口
 * @date 2022/3/2611:54
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = OrderApi.class, retries = 0)
public class OrderApiImpl implements OrderApi {
    @Autowired
    private OrderService orderService;
    @Override
    public JsonResult<GenOrderIdDTO> genOrderId(GenOrderIdRequest genOrderIdRequest) {
        try {
            String userId=genOrderIdRequest.getUserId();
            if (StringUtils.isEmpty(userId)){
                return JsonResult.buildError(OrderErrorCodeEnum.USER_ID_IS_NULL);
            }
            GenOrderIdDTO genOrderIdDTO = orderService.genOrderId(genOrderIdRequest);
            return JsonResult.buildSuccess(genOrderIdDTO);
        }catch (OrderBizException e){
            log.error("biz exception:",e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        }catch (Exception e){
            log.error("system exception", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<CreateOrderDTO> createOrder(CreateOrderRequest createOrderRequest) {
       try {
           CreateOrderDTO order = orderService.createOrder(createOrderRequest);
           return JsonResult.buildSuccess(order);
       }catch (OrderBizException e){
           log.error("biz exception", e);
           return JsonResult.buildError(e.getErrorCode(),e.getErrorMsg());
       }catch (Exception e){
           log.error("system exception", e);
           return JsonResult.buildError(e.getMessage());
       }

    }

    @Override
    public JsonResult<PrePayOrderDTO> prePayOrder(PrePayOrderRequest prePayOrderRequest) {
        try {
            PrePayOrderDTO prePayOrderDTO = orderService.prePayOrder(prePayOrderRequest);
            return JsonResult.buildSuccess(prePayOrderDTO);
        } catch (OrderBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Boolean> payCallback(PayCallbackRequest payCallbackRequest) {
        try {
            orderService.payCallback(payCallbackRequest);
            return JsonResult.buildSuccess(true);
        } catch (OrderBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<RemoveOrderDTO> removeOrders(RemoveOrderRequest removeOrderRequest) {
        try {
            Set<String> orderIdSet=removeOrderRequest.getOrderIds();
            //参数校验
            ParamCheckUtil.checkCollectionNonEmpty(orderIdSet);
            ParamCheckUtil.checkSetMaxSize(orderIdSet, OrderConstants.REMOVE_ORDER_AMX_COUNT, OrderErrorCodeEnum.COLLECTION_PARAM_CANNOT_BEYOND_MAX_SIZE, OrderConstants.REMOVE_ORDER_AMX_COUNT);
            //2、执行具体的移除订单逻辑
            List<String> orderIds = new ArrayList<>(orderIdSet);
            orderService.removeOrders(orderIds);

            return JsonResult.buildSuccess(new RemoveOrderDTO(true));
        } catch (OrderBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<AdjustDeliveryAddressDTO> adjustDeliveryAddress(AdjustDeliveryAddressRequest adjustDeliveryAddressRequest) {
        try {

            //1、参数校验
            ParamCheckUtil.checkStringNonEmpty(adjustDeliveryAddressRequest.getOrderId(), OrderErrorCodeEnum.ORDER_ID_IS_NULL);

            //2、执行具体的调整订单配送地址逻辑
            orderService.adjustDeliveryAddress(adjustDeliveryAddressRequest);

            return JsonResult.buildSuccess(new AdjustDeliveryAddressDTO(true));
        } catch (OrderBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
